const colors = {
  reset: '\x1b[0m',
  info: '\x1b[38;5;213m',    // 粉色
  error: '\x1b[38;5;14m',    // 青色
  gold: '\x1b[38;5;178m'     // 金色
};

// 保存原始的 console.log 和 console.error 引用
const originalConsoleLog = console.log;
const originalConsoleError = console.error;

function getTimestamp() {
  const date = new Date();
  return `[${date.toISOString().substr(11, 8)}.${date.getMilliseconds().toString().padStart(3, '0')}]`;
}

function applyGradient(text, start = 129, end = 213) {
  return [...text].map((char, i) => 
    `\x1b[38;5;${Math.floor(start + (end - start) * (i / text.length))}m${char}`
  ).join('') + colors.reset;
}

function formatMessage(pluginName, message, color) {
  const parts = [
    applyGradient(getTimestamp()),         
    applyGradient('[pippi-logger]'),        
    pluginName ? `${colors.gold}[${pluginName}]${colors.reset}` : '' 
  ].filter(Boolean).join('');

  return `${parts} ${color}${message}${colors.reset}`;
}

class PluginLogger {
  constructor(pluginName) {
    this.pluginName = pluginName;
    
    this.pink = (message) => this.info(message);
    this.gold = (message) => this.#log(originalConsoleLog, message, colors.gold);
  }

  #log(outputFn, message, color) {
    const formatted = formatMessage(this.pluginName, message, color);
    outputFn(formatted);
  }

  info(message) {
    this.#log(originalConsoleLog, message, colors.info); // 默认粉色
  }

  error(message) {
    this.#log(originalConsoleError, message, colors.error); // 错误青色
  }
}

export const createLogger = (name) => new PluginLogger(name);